package com.ruiyi.subway.utils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * 
 * @ClassName: SpringUtils 
 * @Description: spring工具类 
 */
public class SpringUtils implements ApplicationContextAware{
	
	private static ApplicationContext context; //spring容器
	
	/**
	 * 注入spring容器
	 */
	@SuppressWarnings("static-access")
	
	public void setApplicationContext(ApplicationContext contex) throws BeansException {
		this.context=contex;
	}
	
	/**
	 * 取得spring容器
	 * @return
	 */
	public static ApplicationContext getContext(){
		  return context;
	}

	/**
	 * 
	 * <p>Description:取得request</p>
	 * @title getRequest
	 * @return
	 * @return HttpServletRequest
	 */
	public static HttpServletRequest getRequest(){
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	/**
	 * 
	 * <p>Description:取得session</p>
	 * @title getSession
	 * @return
	 * @return HttpSession
	 */
	public static HttpSession getSession(){
		return getRequest().getSession();
	}
	
	/**
	 * 
	 * <p>Description:取得application</p>
	 * @title getApplication
	 * @return
	 * @return ServletContext
	 */
	public static ServletContext getApplication(){
	    return getSession().getServletContext();
	}
	
	public static Object getBean(String beanId){
		ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:/spring/applicationContext.xml");
		return ac.getBean(beanId);
	}
	
	
	/*public static void main(String[] args) {
		IUserService userservice=(IUserService)getBean("userServiceImpl");
		System.out.println(userservice.getUserById(7));
	}
*/
	
}
